/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.report.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import blueprint4j.db.DBConnection;
import blueprint4j.db.DataException;
import blueprint4j.db.Entity;
import blueprint4j.db.FieldForeignKey;
import blueprint4j.db.FieldString;
import blueprint4j.db.FieldUnique;
import blueprint4j.db.TableName;
import blueprint4j.utils.VectorString;


/**
 * This class is an entity that represets a scheduled report that gets emailed
 */

public class ScheduleReportRecipient extends Entity {
	static String VERSION = "$Id: ScheduleReportRecipient.java,v 1.2 2004/08/05 11:38:10 jaspervdb Exp $";

	static final String TABLE_NAME = "d_sch_rpt_recipients";

	public static ScheduleReport CURRENT_USER = null;

	private static VectorString PRIVILEGES = null;

	public TableName table_name = new TableName(TABLE_NAME, this);

	public FieldUnique id = new FieldUnique("id", 0, "The Record Identifier", this);
        public FieldForeignKey schedule_report_id = new FieldForeignKey("schedule_report_id",0,"Schedule Report",26,ScheduleReport.class,this);
	public FieldString recipient = new FieldString("recipient",0,"Email address",100,this);

	public ScheduleReportRecipient() {
		super();
	}

	public ScheduleReportRecipient(DBConnection connection)
	throws DataException, SQLException {
		super(connection);
	}

	public Entity getNewInstance() {
		return new ScheduleReportRecipient();
	}

	public String getVersionString() {
		return VERSION;
	}

	public static ScheduleReportRecipient getRecipient(DBConnection connection,ScheduleReport report)
	throws SQLException {
		return (ScheduleReportRecipient) new ScheduleReportRecipient(connection).find("schedule_report_id = " + Entity.asSQL(report.id.get()));
	}

	public static String getToAddress(DBConnection connection,ScheduleReport report)
	throws SQLException {
		VectorString vs = new VectorString();
		for (
		    ScheduleReportRecipient recipient = ScheduleReportRecipient.getRecipient(connection,report);
		    recipient != null;
		    recipient = (ScheduleReportRecipient) recipient.getNextEntity()
		) {
			vs.add("<"+recipient.recipient.get()+">");
		}
		if (vs.size() > 0) {
			return vs.toString(",");
		}
		return null;

	}
}
